package view;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import resources.ResourceProvider;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *  JPanel to display Login page
 *
 */
public class LoginPage extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6923596370123545779L;
	private JTextField textFieldUsername;
	private JPasswordField textFieldPassword;
	private EmailApplicationView emailApplicationView;

	/*
	 * (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	public void paintComponent(Graphics g) 
    {
		Image image = new ImageIcon("lib" + ResourceProvider.getFilePathSeperator() + "images" + ResourceProvider.getFilePathSeperator() +
				"login_background.jpg").getImage();
		g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }
    
	/**
	 * Create the constructor for login page inputing emial id and password from user to login and asking user 
	 * to create new account.
	 * @param emailApplicationView the email application view reference
	 */
	public LoginPage(final EmailApplicationView emailApplicationView) 
	{
		this.emailApplicationView = emailApplicationView;
	    
	    setBounds(new Rectangle(8, 15, 4, 4));
		setBounds(100, 100, 450, 300);
		//setBounds(100, 100, 150, 100);
		this.setBackground(new Color(204, 204, 204));
		this.setBounds(new Rectangle(8, 15, 4, 4));
		this.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.setLayout(null);
		
		JLabel usernameLabel = new JLabel("EMAIL ID");
		usernameLabel.setName("usernamelabel");
		usernameLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		//usernameLabel.setBounds(new Rectangle(10, 96, 189, 14));
		usernameLabel.setBounds(new Rectangle(250,96,189,14));
		this.add(usernameLabel);
		
		JLabel passwordLabel = new JLabel("PASSWORD");
		passwordLabel.setName("passwordlabel");
		passwordLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		passwordLabel.setBackground(new Color(0, 0, 0));
		//passwordLabel.setBounds(new Rectangle(10, 33, 57, 220));
		passwordLabel.setBounds(new Rectangle(250, 33, 57, 220));
		this.add(passwordLabel);
		
		textFieldUsername = new JTextField();
		textFieldUsername.setName("usernametextbox");
		textFieldUsername.setMaximumSize(new Dimension(40, 40));
		//textFieldUsername.setBounds(new Rectangle(98, 93, 199, 20));
		textFieldUsername.setBounds(new Rectangle(315, 93, 199, 20));
		this.add(textFieldUsername);
		textFieldUsername.setColumns(10);
		
		textFieldPassword = new JPasswordField();
		textFieldPassword.setName("passwordtextbox");
		//textFieldPassword.setBounds(98, 129, 199, 20);	
		textFieldPassword.setBounds(315, 129, 199, 20);
		this.add(textFieldPassword);
		textFieldPassword.setColumns(10);	
		
		JButton btnLogin = new JButton("Login");
		btnLogin.setName("loginbutton");
		//btnLogin.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnLogin.setBounds(370, 165, 89, 23);
		this.add(btnLogin);
		btnLogin.addActionListener(new ActionListener() {
	          
			public void actionPerformed(ActionEvent e)
	          {
                  
	        	  if(textFieldUsername.getText().trim().equals(""))
                  {
                	  final JPanel panel = new JPanel();
                	  JOptionPane.showMessageDialog(panel, "Email id should not be empty!", "Warning",
                	  JOptionPane.WARNING_MESSAGE);
                  }
	        	  else if(textFieldPassword.getPassword().equals(""))
	        	  {
	        		  final JPanel panel = new JPanel();
                	  JOptionPane.showMessageDialog(panel, "Password should not be empty!", "Warning",
                	  JOptionPane.WARNING_MESSAGE);
	        	  }
	        	  else
	        	  {
		        	  String emailId = textFieldUsername.getText().trim();
		        	 
	                  boolean isRegistered = emailApplicationView.getController().requestIsEmailIdRegistered(emailId);
	                  
	            	  if(isRegistered)
	                  {
	            	      String password = (new String(textFieldPassword.getPassword())).trim();
	            	      boolean isValid = emailApplicationView.getController().requestIsValidUser(emailId, password);	
	            	      if(isValid)
	            	      {
	            	          LoginPage.this.emailApplicationView.getController().loginSuccess(emailId);
                              LoginPage.this.emailApplicationView.initialize();   
	            	      }
	            	      else
	            	      {
	            	          JPanel panel = new JPanel();
	                          JOptionPane.showMessageDialog(panel, "Password is invalid!", "Warning",
	                          JOptionPane.WARNING_MESSAGE); 
	            	      }   	                          
	                  } 
	            	  else
	                  {
	                     JPanel panel = new JPanel();
	                 	 JOptionPane.showMessageDialog(panel, "E-mail Id is not registered, please register!", "Warning",
	                 	 JOptionPane.WARNING_MESSAGE);    	  
	                  } 
	        	  }
	          }
	      });
		
	
		JButton btnNewButton_1 = new JButton("Create a new account");
		btnNewButton_1.setName("createnewbutton");
		btnNewButton_1.setBounds(315, 219, 199, 23);
		this.add(btnNewButton_1);
		btnNewButton_1.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e)
            {
                LoginPage.this.emailApplicationView.displayRegister();     
            }
        });
		
		JLabel lbloutlook = new JLabel("Please Login");
		lbloutlook.setBackground(new Color(0, 0, 0));
		lbloutlook.setForeground(new Color(0, 0, 0));
		lbloutlook.setFont(new Font("Times New Roman", Font.BOLD, 18));
		lbloutlook.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
		lbloutlook.setBounds(350, 0, 189, 54);
		this.add(lbloutlook);		
	}
}
